import {
  Controller,
  Post,
  Get,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
} from "@nestjs/common";
import { ChatMessageService } from "../services/chat-message.service";
import { AuthGuard } from "../auth/auth.guard";

@Controller("chat")
@UseGuards(AuthGuard)
export class ChatMessageController {
  constructor(private readonly chatMessageService: ChatMessageService) {}

  /**
   * 获取登录后的消息列表 (act_id: 66)
   * GET /chat/login-messages
   */
  @Get("login-messages")
  async getLoginMessages(@Query("limit") limit?: number) {
    try {
      const messages = await this.chatMessageService.getLoginMessages(
        limit || 50
      );
      return {
        act_id: 66,
        code: 0,
        data: messages,
      };
    } catch (error) {
      return {
        act_id: 66,
        code: -1,
        msg: "获取消息失败",
        data: [],
      };
    }
  }

  /**
   * 发送消息 (c: 260)
   * POST /chat/send
   */
  @Post("send")
  async sendMessage(
    @Body()
    messageData: {
      content: string;
      senderName: string;
      senderCharacterId: number;
      messageType: number;
      areaId?: number;
      factionId?: number;
    }
  ) {
    try {
      const message = await this.chatMessageService.sendMessage(messageData);
      return {
        act_id: 260,
        code: 0,
        data: message,
      };
    } catch (error: any) {
      return {
        act_id: 260,
        code: -1,
        msg: error?.message || "发送消息失败",
      };
    }
  }

  /**
   * 获取区域消息 (t: 1)
   * GET /chat/area/:areaId
   */
  @Get("area/:areaId")
  async getAreaMessages(
    @Param("areaId") areaId: number,
    @Query("limit") limit?: number
  ) {
    try {
      const messages = await this.chatMessageService.getAreaMessages(
        areaId,
        limit || 20
      );
      return {
        act_id: 1001,
        code: 0,
        data: messages,
      };
    } catch (error) {
      return {
        act_id: 1001,
        code: -1,
        msg: "获取区域消息失败",
        data: [],
      };
    }
  }

  /**
   * 获取商业消息 (t: 5)
   * GET /chat/business
   */
  @Get("business")
  async getBusinessMessages(@Query("limit") limit?: number) {
    try {
      const messages = await this.chatMessageService.getBusinessMessages(
        limit || 20
      );
      return {
        act_id: 1002,
        code: 0,
        data: messages,
      };
    } catch (error) {
      return {
        act_id: 1002,
        code: -1,
        msg: "获取商业消息失败",
        data: [],
      };
    }
  }

  /**
   * 获取帮派消息 (t: 4)
   * GET /chat/faction/:factionId
   */
  @Get("faction/:factionId")
  async getFactionMessages(
    @Param("factionId") factionId: number,
    @Query("limit") limit?: number
  ) {
    try {
      const messages = await this.chatMessageService.getFactionMessages(
        factionId,
        limit || 20
      );
      return {
        act_id: 1003,
        code: 0,
        data: messages,
      };
    } catch (error) {
      return {
        act_id: 1003,
        code: -1,
        msg: "获取帮派消息失败",
        data: [],
      };
    }
  }

  /**
   * 获取系统消息
   * GET /chat/system
   */
  @Get("system")
  async getSystemMessages(@Query("limit") limit?: number) {
    try {
      const messages = await this.chatMessageService.getSystemMessages(
        limit || 20
      );
      return {
        act_id: 1004,
        code: 0,
        data: messages,
      };
    } catch (error) {
      return {
        act_id: 1004,
        code: -1,
        msg: "获取系统消息失败",
        data: [],
      };
    }
  }

  /**
   * 删除消息
   * DELETE /chat/:messageId
   */
  @Delete(":messageId")
  async deleteMessage(
    @Param("messageId") messageId: number,
    @Body() body: { senderCharacterId: number }
  ) {
    try {
      const result = await this.chatMessageService.deleteMessage(
        messageId,
        body.senderCharacterId
      );
      return {
        act_id: 10015,
        code: 0,
        data: result,
      };
    } catch (error: any) {
      return {
        act_id: 10015,
        code: -1,
        msg: error?.message || "删除消息失败",
      };
    }
  }

  /**
   * 清理过期消息
   * POST /chat/cleanup
   */
  @Post("cleanup")
  async cleanupOldMessages() {
    try {
      const count = await this.chatMessageService.cleanupOldMessages();
      return {
        act_id: 1006,
        code: 0,
        data: { cleanedCount: count },
      };
    } catch (error) {
      return {
        act_id: 1006,
        code: -1,
        msg: "清理过期消息失败",
      };
    }
  }

  /**
   * 创建默认系统消息
   * POST /chat/init-system
   */
  @Post("init-system")
  async createDefaultSystemMessages() {
    try {
      const result =
        await this.chatMessageService.createDefaultSystemMessages();
      return {
        act_id: 1007,
        code: 0,
        data: { success: result },
      };
    } catch (error) {
      return {
        act_id: 1007,
        code: -1,
        msg: "创建默认系统消息失败",
      };
    }
  }
}
